using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace JufGame
{
	public class OctreeBuilder : MonoBehaviour
	{
		public GameObject[] Objects;
		public float MinNodeSize = 1;
		public MyGraph<OctreeNode, int> NavGraph;
		public Octree Octree;
		[SerializeField] private bool ShowNode = true;
		[SerializeField] private bool ShowEdge = true;
		[SerializeField] private bool ShowGraphNode = true;
		private void Awake()
		{
			NavGraph = new MyGraph<OctreeNode, int>();
			Octree = new Octree(Objects, MinNodeSize, NavGraph);
		}
		private void OnDrawGizmosSelected()
		{
			if(Application.isPlaying)
			{
				if(ShowNode)
					Octree.RootNode.Draw();
				DrawGraph();
			}
		}
		private void DrawGraph()
		{
			if(ShowEdge)
			{
				foreach(var edge in NavGraph.EdgeList)
				{
					Debug.DrawLine(edge.Key.Item1.NodeBounds.center,
					edge.Key.Item2.NodeBounds.center, Color.red);
				}
			}
			if(ShowGraphNode)
			{
				foreach(var node in NavGraph.NodeSet)
				{
					Gizmos.color = new Color(1, 1, 0);
					Gizmos.DrawWireSphere(node.NodeBounds.center, 0.25f);
				}
			}
		}
	}
}
